package com.hilaver.dzmis.util.resources;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class I18nResourcesHandler {

	// resource message package
	private final static String RESOURCE_PACKAGE = "com.hilaver.dzmis.util.resources";

	// message file name prefix
	private final static String MESSAGE_NAME = "message";

	// language type
	public final static Locale LOCAL_TYPE_FR = new Locale(Locale.JAPAN
			.getLanguage(), Locale.JAPAN.getCountry()); // French
	public final static Locale LOCAL_TYPE_ZH = new Locale(Locale.CHINA
			.getLanguage(), Locale.CHINA.getCountry()); // Chinese
	public final static Locale LOCAL_TYPE_EN = new Locale(Locale.US
			.getLanguage(), Locale.US.getCountry()); // English

	// For French
	private static ResourceBundle RESOURCE_BUNDLE_FR_FR = null;

	// For Chinese
	private static ResourceBundle RESOURCE_BUNDLE_ZH_CN = null;

	// For English
	private static ResourceBundle RESOURCE_BUNDLE_EN_US = null;

	//initialize three language java resource bundle
	private static void init() {
		try {
			if (RESOURCE_BUNDLE_FR_FR == null) {
				RESOURCE_BUNDLE_FR_FR = ResourceBundle.getBundle(
						RESOURCE_PACKAGE + "." + MESSAGE_NAME, LOCAL_TYPE_FR);
			}

			if (RESOURCE_BUNDLE_ZH_CN == null) {
				RESOURCE_BUNDLE_ZH_CN = ResourceBundle.getBundle(
						RESOURCE_PACKAGE + "." + MESSAGE_NAME, LOCAL_TYPE_ZH);
			}

			if (RESOURCE_BUNDLE_EN_US == null) {
				RESOURCE_BUNDLE_EN_US = ResourceBundle.getBundle(
						RESOURCE_PACKAGE + "." + MESSAGE_NAME, LOCAL_TYPE_EN);
			}
		} catch (MissingResourceException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Give the Locale message, and you can get the corresponding ResourceBundle.
	 * French, Chinese and English are supported.
	 * 
	 * @param locale - French, Chinese or English
	 * @return ResourceBundle
	 */
	public static ResourceBundle getResourceBundleFromLocale(Locale locale) {
		init();
		ResourceBundle rb = null;
		if (LOCAL_TYPE_FR.equals(locale)) {
			rb = RESOURCE_BUNDLE_FR_FR;
		} else if (LOCAL_TYPE_ZH.equals(locale)) {
			rb = RESOURCE_BUNDLE_ZH_CN;
		} else {
			rb = RESOURCE_BUNDLE_EN_US;
		}
		return rb;
	}

	/**
	 * Give the Locale message and the key, and you can get the corresponding locale message.
	 * French, Chinese and English are supported.
	 * 
	 * @param key - the message unified key
	 * 		  locale - French, Chinese or English
	 * @return String
	 */
	public static String get(String key, Locale locale) {
		init();
		String value = "";
		if (LOCAL_TYPE_FR.equals(locale)) {
			value = RESOURCE_BUNDLE_FR_FR.getString(key);
		} else if (LOCAL_TYPE_ZH.equals(locale)) {
			value = RESOURCE_BUNDLE_ZH_CN.getString(key);
		} else {
			value = RESOURCE_BUNDLE_EN_US.getString(key);
		}
		return value.trim();
	}

	/**
	 * Give the the message key, and you can get the corresponding English message.
	 * Here we think the English message is the default messaage file. 
	 * 
	 * @param key - the message unified key
	 * @return String
	 */
	public static String get(String key) {
		init();
		String value = "";
		value = RESOURCE_BUNDLE_EN_US.getString(key);
		return value.trim();
	}
}
